package com.foxcai.novel.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.foxcai.novel.base.DBConnection;
import com.foxcai.novel.http.EasouHttpCommAction;
import com.foxcai.novel.util.Convert;

public class GetNovelListToDb {

	public static Boolean InsertToDb(String xml) {

		Connection conn = null;

		PreparedStatement pstm = null;

		Statement stam = null;

		ResultSet rs = null;

		try {
			Document doc = DocumentHelper.parseText(xml);

			Element root = doc.getRootElement();
			Element nodes = root.element("data");

			System.out.println("成功标识:" + root.elementText("success"));
			System.out.println("总数:" + root.elementText("total"));

			if (root.elementText("success").equals("true")) {
				conn = DBConnection.newInstance().getConnection();

				Element foo;
				String sql = "insert into NovelList (NovId,category,novel,author,NovAcc,UpdTime) values (?,?,?,?,?,?)";
				String qrySql;
				for (Iterator i = nodes.elementIterator("item"); i.hasNext();) {
					foo = (Element) i.next();

					qrySql = "select NovId from NovelList where NovId=\""
							+ foo.elementText("id") + "\"";
					stam = conn.createStatement();

					rs = stam.executeQuery(qrySql);

					if (rs.next()) {
						qrySql = "update NovelList set NovAcc=?,UpdTime=? where NovId=?";
						pstm = conn.prepareStatement(qrySql);
						pstm.setInt(1, Integer.parseInt(foo.elementText("access")));
						pstm.setString(2, Convert.TimeConvert(Long.parseLong(foo.elementText("time"))));
						pstm.setInt(3, Integer.parseInt(foo.elementText("id")));
						pstm.execute();
						System.out.println("update");
					} else {

						pstm = conn.prepareStatement(sql);

						pstm.setInt(1, Integer.parseInt(foo.elementText("id")));
						pstm.setString(2, foo.elementText("category"));
						pstm.setString(3, foo.elementTextTrim("novel"));
						pstm.setString(4, foo.elementTextTrim("author"));
						pstm.setInt(5, Integer.parseInt(foo.elementText("access")));
						pstm.setString(6, Convert.TimeConvert(Long.parseLong(foo.elementText("time"))));

						pstm.execute();
						System.out.println("insert");
					}

				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstm != null) {
					pstm.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

		return null;

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String reqUrl = "http://b1.easou.com/api/uc/serial_novel.m";

		String reqParams = null;
		String resultXml = null;
		EasouHttpCommAction ehca = new EasouHttpCommAction();
		long beg = System.currentTimeMillis();
		for (int i = 1; i <= 424; i++) {
			System.out.println("第" + i + "页");
			reqParams = "cid=hubb&recently=1000&begin=" + i + "&count=10";
			resultXml = ehca.send(reqParams,reqUrl);
			InsertToDb(resultXml);
		}
		long curTime = System.currentTimeMillis();
		long intvl = curTime - beg;
		System.out.println("3000条数据耗时：" + intvl + "毫秒！！");

	}
}
